package com.kgc.skconsumer.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.collection.pojo.SalesCollection;
import com.kgc.collection.pojo.SalesCollectionRefund;
import com.kgc.skconsumer.feign.CwWorkFlowFeign;
import com.kgc.skconsumer.feign.SalesCollectionFeign;
import com.kgc.skconsumer.feign.SalesCollectionRefundFeign;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("SalesCollectionRefundController")
public class SalesCollectionRefundController {

    private Logger logger = LoggerFactory.getLogger(SalesCollectionRefundController.class);

    @Autowired
    private SalesCollectionRefundFeign salesCollectionRefundFeign;

    @Autowired
    private CwWorkFlowFeign  cwWorkFlowFeign;

    @Autowired
    private SalesCollectionFeign salesCollectionFeign;

    @RequestMapping("/selectSalesCollectionRefundByPage")
    public ResponseMsg selectSalesCollectionRefundByPage(@RequestBody Map<String, Object> map) {
        logger.info("SalesCollectionRefundController selectSalesCollectionRefundByPage params {}", map.toString());
        ResponseMsg responseMsg = salesCollectionRefundFeign.selectSalesCollectionRefundByPage(map);
        logger.debug("SalesCollectionRefundController selectSalesCollectionRefundByPage responseMsg {}", responseMsg);
        return responseMsg;
    }

    @RequestMapping("/insertSalesCollectionRefund")
    public  ResponseMsg insertSalesCollectionRefund(@RequestBody Map<String, Object> map) {
        logger.info("SalesCollectionRefundController saveSalesCollectionRefund params {}", map.toString());
        ResponseMsg responseMsg = salesCollectionRefundFeign.insertSalesCollectionRefund(map);
        ObjectMapper mapper = new ObjectMapper();
        SalesCollectionRefund salesCollectionRefund = mapper.convertValue(map.get("salesCollectionRefund"), SalesCollectionRefund.class);
        Map<String,Object> variables = new HashMap<>();
        variables.put("code",salesCollectionRefund.getDocCode());//单据编号
        variables.put("type",salesCollectionRefund.getTypeId());//流程类型
        //创建一个流程实例
        cwWorkFlowFeign.startProcess(variables);
        logger.debug("SalesCollectionRefundController insertSalesCollectionRefund responseMsg {}", responseMsg);
        return  responseMsg;
    }

    @RequestMapping("/updateSalesCollectionRefund")
    public  ResponseMsg updateSalesCollectionRefund(@RequestBody Map<String, Object> map) {
        logger.info("SalesCollectionRefundController updateSalesCollectionRefund params {}", map.toString());
        ResponseMsg responseMsg = salesCollectionRefundFeign.updateSalesCollectionRefund(map);
        logger.debug("SalesCollectionRefundController updateSalesCollectionRefund responseMsg {}", responseMsg);
        return  responseMsg;
    }

    @RequestMapping("/deleteSalesCollectionRefund")
    public  ResponseMsg deleteSalesCollectionRefund(@RequestBody Map<String, Object> map) {
        logger.info("SalesCollectionRefundController deleteSalesCollectionRefund params {}", map.toString());
        ResponseMsg responseMsg = salesCollectionRefundFeign.deleteSalesCollectionRefund(map);
        logger.debug("SalesCollectionRefundController deleteSalesCollectionRefund responseMsg {}", responseMsg);
        return responseMsg;
    }

    @RequestMapping("/checkSalesCollectionRefund")
    public  ResponseMsg checkSalesCollectionRefund(@RequestBody Map<String, Object> map) {
        logger.info("SalesCollectionRefundController checkSalesCollectionRefund params {}", map.toString());
        ResponseMsg responseMsg = new ResponseMsg();
        Map<String,Object> variables = new HashMap<>();
        variables.put("code",(String) map.get("docCode"));//单据编号
        variables.put("checkManId",(Integer) map.get("checkManId"));//审批人
        variables.put("checkManName",(String) map.get("checkManName"));//审批人
        variables.put("checkComment",(String) map.get("checkComment"));//审批意见
        variables.put("checkDate",new Date());//审批时间
        variables.put("checkRes",(boolean)map.get("checkRes"));//审批结果
        com.kgc.activiti.util.ResponseMsg msg=  cwWorkFlowFeign.competeTask(variables);
        responseMsg.setCode(msg.getCode());
        responseMsg.setMsg(msg.getMsg());
        logger.debug("SalesCollectionRefundController checkSalesCollectionRefund responseMsg {}", responseMsg);
        return responseMsg;
    }

    @RequestMapping("/selectSalesCollectionRefundByCode")
    public  ResponseMsg selectSalesCollectionRefundByCode(@RequestBody Map<String, Object> map) {
        logger.info("SalesCollectionRefundController selectSalesCollectionRefundByCode params {}", map.toString());
        return  salesCollectionRefundFeign.selectSalesCollectionRefundByCode(map);
    }

    @RequestMapping("/selectSalesCollectionRefundDetailByCode")
    public  ResponseMsg selectSalesCollectionRefundDetailByCode(@RequestBody Map<String, Object> map) {
        logger.info("SalesCollectionRefundController selectSalesCollectionRefundDetailByCode params {}", map.toString());
        return  salesCollectionRefundFeign.selectSalesCollectionRefundDetailByCode(map);
    }

}
